package blackstar.performance.logger.info;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author raywu (ayufox@gmail.com)
 */
public class LoadChainNode
{
	private String className;
	private String method;
	private long beginTimeNano;
	private long endTimeNano;
	private List<LoadChainNode> children;

	public LoadChainNode(String className, String method)
	{
		this.className = className;
		this.method = method;
	}

	public void addChild(LoadChainNode node)
	{
		if (this.children == null)
		{
			this.children = new ArrayList<LoadChainNode>();
		}
		this.children.add(node);
	}

	public void start(long time)
	{
		this.beginTimeNano = time;
	}

	public void start()
	{
		start(System.nanoTime());
	}

	public void stop(long time)
	{
		this.endTimeNano = time;
	}

	public void stop()
	{
		stop(System.nanoTime());
	}

	public long getTimeNano()
	{
		return (this.endTimeNano - this.beginTimeNano);
	}

	public String getFullName()
	{
		return this.className + "." + this.method;
	}

	public String getClassName()
	{
		return className;
	}

	public String getMethod()
	{
		return method;
	}

	public List<LoadChainNode> getChildren()
	{
		return children;
	}
	
	/**
	 * @param visitor
	 */
	public void visitDepthFirst(INodeVisitor visitor)
	{
		visitor.visitBegin(this);
		if (this.children != null)
		{
			for (LoadChainNode node : this.children)
			{
				node.visitDepthFirst(visitor);
			}
		}
		visitor.visitEnd(this);
	}
}
